/**
 * @file       BaseNetworkConfigPanel.cpp
 * @brief      TODO
 * @author     Huabo Zheng
 * @date       Feb 3, 2012
 * @Copyright  2012, www.netgear.com Inc. All rights reserved.
 */

#include "BaseNetworkConfigPanel.h"
#include "errorcode.h"
#include "IPEditField.h"
#include "Label.h"
#include "Button.h"
#include "IEventListener.h"
#include "KeyboardListener.h"
#include <pthread.h>
#include <string.h>
#include "MenuButton.h"

namespace NS_LCD_AFW
{

CBaseNetworkConfigPanel::CBaseNetworkConfigPanel() :
		m_pIpAddress(NULL), m_pNetMask(NULL), m_pGateWay(NULL)
{
}

CBaseNetworkConfigPanel::~CBaseNetworkConfigPanel()
{
	m_pIpAddress = NULL;
	m_pNetMask = NULL;
	m_pGateWay = NULL;
}

int CBaseNetworkConfigPanel::onCreate()
{
	this->cleanChildObject();
	const static char * pTmp[] =
	{ IP_ADDRESS, NET_MASK, GATEWAY_ADDRESS };
	CLabel *pLabel = NULL;
	for (int i = 0, j = 0; i < 3; ++i, j += 2)
	{
		OBJ_NEW(CLabel, pLabel);
		if (NULL == pLabel)
		{
			return ERR_NO_MEMORY;
		}
		pLabel->create(pTmp[i], CRect(0, j, LCD_WIDTH - 2, 1), this);
		pLabel->setText(pTmp[i]);
		this->add(pLabel);
	}

	OBJ_NEW(CIPEditField, this->m_pIpAddress);
	m_pIpAddress->create("ip", CRect(4, 1, LCD_WIDTH - 4, IP_ADDRESS_HEIGHT),
			this);
	this->add(m_pIpAddress);
	OBJ_NEW(CIPEditField, this->m_pNetMask);
	m_pNetMask->create("netmask", CRect(4, 3, LCD_WIDTH - 4, IP_ADDRESS_HEIGHT),
			this);
	this->add(m_pNetMask);
	OBJ_NEW(CIPEditField, this->m_pGateWay);
	m_pGateWay->create("gateway", CRect(4, 5, LCD_WIDTH - 4, IP_ADDRESS_HEIGHT),
			this);
	this->add(m_pGateWay);

	CMenuButton *pSaveBtn = NULL;
	OBJ_NEW(CMenuButton, pSaveBtn);
	pSaveBtn->create("saveButton", CRect(5, 6, 4, 1), this);
	pSaveBtn->setText("Save");
	IEventListener *pListener = new CKeyboardListener(this,
			ENTER_KEY_PRESSED_EVENT_ID,
			(AFW_EventCallBack) (&CBaseNetworkConfigPanel::onSaveBtnClick));
	pSaveBtn->addEventListener(pListener);
	this->add(pSaveBtn);

	CMenuButton *pCancleBtn = NULL;
	OBJ_NEW(CMenuButton, pCancleBtn);
	pCancleBtn->create("cancelButton", CRect(10, 6, 6, 1), this);
	pCancleBtn->setText("Cancel");
	IEventListener *pCancelListener = new CKeyboardListener(this,
			ENTER_KEY_PRESSED_EVENT_ID,
			(AFW_EventCallBack) (&CBaseNetworkConfigPanel::onCancelBtnClick));
	pCancleBtn->addEventListener(pCancelListener);
	this->add(pCancleBtn);
	return CPanel::onCreate();
}

int CBaseNetworkConfigPanel::onEnterKeyPress()
{
	return CPanel::onEnterKeyPress();
}

int CBaseNetworkConfigPanel::onEscKeyPress()
{
	return CPanel::onEscKeyPress();
}

int CBaseNetworkConfigPanel::onUpKeyPress()
{
	return CPanel::onUpKeyPress();
}

int CBaseNetworkConfigPanel::onDownKeyPress()
{
	return CPanel::onDownKeyPress();
}

int CBaseNetworkConfigPanel::setIpAddress(const std::string & ipAddr)
{
	if (NULL == this->m_pIpAddress)
	{
		return ERR_NULL_POINTER;
	}
	return m_pIpAddress->setIP(ipAddr);
}

int CBaseNetworkConfigPanel::setNetMask(const std::string & netMaskAddr)
{
	if (NULL == this->m_pNetMask)
	{
		return ERR_NULL_POINTER;
	}
	return m_pNetMask->setIP(netMaskAddr);
}

int CBaseNetworkConfigPanel::setGateway(const std::string & gatewayAddr)
{
	if (NULL == this->m_pGateWay)
	{
		return ERR_NULL_POINTER;
	}
	return m_pGateWay->setIP(gatewayAddr);
}

std::string CBaseNetworkConfigPanel::getIpAddress() const
{
	if (NULL == this->m_pIpAddress)
	{
		return "";
	}
	return m_pIpAddress->getText();
}

std::string CBaseNetworkConfigPanel::getNetMask() const
{
	if (NULL == this->m_pNetMask)
	{
		return "";
	}
	return m_pNetMask->getText();
}

std::string CBaseNetworkConfigPanel::getGateway() const
{
	if (NULL == this->m_pGateWay)
	{
		return "";
	}
	return m_pGateWay->getText();
}

void CBaseNetworkConfigPanel::onSaveBtnClick(int id, int param)
{
	this->onSave();
	this->close();
}

void CBaseNetworkConfigPanel::onCancelBtnClick(int id, int param)
{
	this->onCancel();
	this->close();
}
void CBaseNetworkConfigPanel::onCancel()
{
}

void CBaseNetworkConfigPanel::onSave()
{
}
}

/* namespace NS_LCD_AFW */
